package android07.newtop.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;

import com.jude.beam.bijection.RequiresPresenter;
import com.jude.beam.expansion.data.BeamDataActivity;

import java.io.InputStream;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import android07.newtop.R;
import android07.newtop.model.bean.Danmu;
import android07.newtop.model.bean.PictureDetail;
import android07.newtop.presenter.PictureShowActivityPresenter;
import android07.newtop.utils.DanmuControl;
import android07.newtop.utils.MyUtil;
import android07.newtop.view.CenteredImageSpan;
import android07.newtop.view.CircleDrawable;
import master.flame.danmaku.controller.IDanmakuView;
import master.flame.danmaku.danmaku.loader.ILoader;
import master.flame.danmaku.danmaku.loader.IllegalDataException;
import master.flame.danmaku.danmaku.loader.android.DanmakuLoaderFactory;
import master.flame.danmaku.danmaku.model.BaseDanmaku;
import master.flame.danmaku.danmaku.model.DanmakuTimer;
import master.flame.danmaku.danmaku.model.IDanmakus;
import master.flame.danmaku.danmaku.model.IDisplayer;
import master.flame.danmaku.danmaku.model.android.BaseCacheStuffer;
import master.flame.danmaku.danmaku.model.android.DanmakuContext;
import master.flame.danmaku.danmaku.model.android.Danmakus;
import master.flame.danmaku.danmaku.model.android.SpannedCacheStuffer;
import master.flame.danmaku.danmaku.parser.BaseDanmakuParser;
import master.flame.danmaku.danmaku.parser.IDataSource;
import master.flame.danmaku.danmaku.parser.android.BiliDanmukuParser;

/**
 * 图片浏览页面
 * <p>
 * <p>
 * <p>
 * Created by hy on 2016/10/20
 */
@RequiresPresenter(PictureShowActivityPresenter.class)
public class PictureShowActivity extends BeamDataActivity<PictureShowActivityPresenter, PictureDetail> {
    private static final String TAG = PictureShowActivity.class.getSimpleName();
    Toolbar toolbar;
    private AppBarLayout appBarLayout;
    public RecyclerView recyclerview;
    private LinearLayout llroot;
    private IDanmakuView mDanmakuView;
    private DanmuControl mDanmuControl;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }
        setContentView(R.layout.picture_show_activity);

        mDanmuControl = new DanmuControl(this);
        initView();


        //设置toolbar
        setSupportActionBar(toolbar);

        AppBarLayout.LayoutParams param = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
        param.setMargins(0, 0, 0, 0);
        toolbar.setLayoutParams(param);

        blur();
    }

    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        this.llroot = (LinearLayout) findViewById(R.id.ll_root);
        this.recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
        this.appBarLayout = (AppBarLayout) findViewById(R.id.appBarLayout);
        initDanmuView();

    }


    private void initDanmuView() {
        mDanmakuView = (IDanmakuView) findViewById(R.id.sv_danmaku);
        CheckBox cb_danmu = (CheckBox) findViewById(R.id.cb_danmu);
        mDanmuControl.setDanmakuView(mDanmakuView);
        cb_danmu.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    setData();
                } else {

                }
            }
        });

    }

    private void setData() {
        List<Danmu> danmus = new ArrayList<>();
        Danmu danmu1 = new Danmu(0, 1, "Comment", R.drawable.picture_danmu_ic_default_header, "asdasdaa");
        Danmu danmu2 = new Danmu(0, 2, "Comment", R.drawable.picture_danmu_ic_default_header, "这是一条弹幕啦啦啦");
        Danmu danmu3 = new Danmu(0, 3, "Comment", R.drawable.picture_danmu_ic_default_header, "sadasdasd");
        Danmu danmu4 = new Danmu(0, 1, "Comment", R.mipmap.ic_launcher, "这又是一条弹幕啦啦啦");
        Danmu danmu5 = new Danmu(0, 2, "Like", R.mipmap.ic_launcher, "");
        Danmu danmu6 = new Danmu(0, 3, "Comment", R.mipmap.ic_launcher, "这还是一条弹幕啦啦啦");
        danmus.add(danmu1);
        danmus.add(danmu1);
        danmus.add(danmu1);
        danmus.add(danmu1);
        danmus.add(danmu1);
        danmus.add(danmu1);
        danmus.add(danmu1);
        danmus.add(danmu1);
        danmus.add(danmu1);
        danmus.add(danmu1);
        danmus.add(danmu1);

        danmus.add(danmu2);
        danmus.add(danmu3);
        danmus.add(danmu4);
        danmus.add(danmu5);
        danmus.add(danmu6);
        Collections.shuffle(danmus);

        mDanmuControl.addDanmuList(danmus);
    }

    /**
     * 加载背景磨砂图片
     */
    private void blur() {
        new AsyncTask<Void, Void, Void>() {
            Drawable wallPaperBlurDrawable;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected Void doInBackground(Void... params) {
//                mDensity = getResources().getDisplayMetrics().density;

                wallPaperBlurDrawable = MyUtil.getWallPaperBlurDrawable(PictureShowActivity.this);
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                llroot.setBackground(wallPaperBlurDrawable);
                super.onPostExecute(aVoid);
            }
        }.execute();
    }

//

    /**
     * 给其他类获得TableLayout的方法
     *
     * @return
     */
    public Toolbar getToolbar() {
        return toolbar;
    }

    @Override
    protected void onPause() {
        super.onPause();
        mDanmuControl.pause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mDanmuControl.resume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mDanmuControl.destroy();
    }

//    private RecyclerView mRecyclerView;
//    private ImageView mBlurView;
//    private List<Integer> mList = new ArrayList<>();
//    private CardScaleHelper mCardScaleHelper = null;
//    private Runnable mBlurRunnable;
//    private int mLastPos = -1;
//

//
//    private void init() {
//        for (int i = 0; i < 10; i++) {
//            mList.add(R.drawable.pic4);
//            mList.add(R.drawable.pic5);
//            mList.add(R.drawable.pic6);
//        }
//
//        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
//        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
//        mRecyclerView.setLayoutManager(linearLayoutManager);
//        mRecyclerView.setAdapter(new CardAdapter(mList));
//        // mRecyclerView绑定scale效果
//        mCardScaleHelper = new CardScaleHelper();
//        mCardScaleHelper.setCurrentItemPos(2);
//        mCardScaleHelper.attachToRecyclerView(mRecyclerView);
//
//        initBlurBackground();
//    }
//
//    private void initBlurBackground() {
//        mBlurView = (ImageView) findViewById(R.id.blurView);
//        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
//                super.onScrollStateChanged(recyclerView, newState);
//                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
//                    notifyBackgroundChange();
//                }
//            }
//        });
//
//        notifyBackgroundChange();
//    }
//
//    private void notifyBackgroundChange() {
//        if (mLastPos == mCardScaleHelper.getCurrentItemPos()) return;
//        mLastPos = mCardScaleHelper.getCurrentItemPos();
//        final int resId = mList.get(mCardScaleHelper.getCurrentItemPos());
//        mBlurView.removeCallbacks(mBlurRunnable);
//        mBlurRunnable = new Runnable() {
//            @Override
//            public void run() {
//                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resId);
//                ViewSwitchUtils.startSwitchBackgroundAnim(mBlurView, BlurBitmapUtils.getBlurBitmap(mBlurView.getContext(), bitmap, 15));
//            }
//        };
//        mBlurView.postDelayed(mBlurRunnable, 500);
//    }
}
